// Temp funtion until i manage to return a float array from the npapi dll
function getMatrixTemp(trackIRObject) {
    matrix = [1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1];
    matrix[0] = parseFloat(trackIRObject.getMatrix0);
    matrix[1] = parseFloat(trackIRObject.getMatrix1);
    matrix[2] = parseFloat(trackIRObject.getMatrix2);
    matrix[3] = parseFloat(trackIRObject.getMatrix3);
    matrix[4] = parseFloat(trackIRObject.getMatrix4);
    matrix[5] = parseFloat(trackIRObject.getMatrix5);
    matrix[6] = parseFloat(trackIRObject.getMatrix6);
    matrix[7] = parseFloat(trackIRObject.getMatrix7);
    matrix[8] = parseFloat(trackIRObject.getMatrix8);
    matrix[9] = parseFloat(trackIRObject.getMatrix9);
    matrix[10] = parseFloat(trackIRObject.getMatrix10);
    matrix[11] = parseFloat(trackIRObject.getMatrix11);
    matrix[12] = parseFloat(trackIRObject.getMatrix12);
    matrix[13] = parseFloat(trackIRObject.getMatrix13);
    matrix[14] = parseFloat(trackIRObject.getMatrix14);
    matrix[15] = parseFloat(trackIRObject.getMatrix15);
    return matrix.join(",");
}

function trackIRController() {
    this.tirAddNPAPIelement = function() {
        var trackIRDLL = document.createElement("embed");
        trackIRDLL.setAttribute("id","trackIRDLL");
        trackIRDLL.setAttribute("type","application/mozilla-nptrackir-plugin");
        trackIRDLL.setAttribute("width",0);
        trackIRDLL.setAttribute("height",0);
        trackIRDLL.style.position = "absolute";
        trackIRDLL.style.top = 0+"px";
        trackIRDLL.style.left = 0+"px";
        trackIRDLL.style.display = "block";
        document.body.appendChild(trackIRDLL);
    };

    this.tirDelNPAPIelement = function() {
        var trackIRDLL = document.getElementById("trackIRDLL");
        if(trackIRDLL != null) {
            document.body.removeChild(trackIRDLL);
        }
    };

    this.tirGetMatrix = function() {
        var matrix = [1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1];
        if(this.trackIRObject != null) {
            matrix = this.trackIRObject.tirGetMatrix();
            if(this.type == "npapi") {
                matrix = getMatrixTemp(this.trackIRObject);
            }
            matrix = matrix.split(",");
        }
        return matrix;
    };

    this.tirRecenter = function() {
        if(this.trackIRObject != null) {
            this.trackIRObject.tirRecenter();
        }
    };

    this.tirIsCameraInitialized = function() {
        if(this.trackIRObject != null) {
            return this.trackIRObject.tirIsCameraInitialized();
        }
    };

    this.tirIsCameraRunning = function() {
        if(this.trackIRObject != null) {
            return this.trackIRObject.tirIsCameraRunning();
        }
    };

    this.tirSetNote = function() {
        if(this.trackIRObject != null) {
            var data = "";
            data += "This website is TrackIR enabled, as such\n";
            data += "if you have your TrackIR on and dont see\n";
            data += "anything, press C to recenter\n";
            alert(data);
        }
    }

    try {
        this.trackIRObject = new ActiveXObject("trackIRDLL.ActiveX");
        this.trackIRObject.tirRecenter();
        this.type = "activex";
    } catch(err) {
        try {
            this.tirAddNPAPIelement();
            this.trackIRObject = document.getElementById('trackIRDLL');
            this.trackIRObject.tirIsCameraInitialized();
            this.type = "npapi";
        } catch(err) {
            this.tirDelNPAPIelement();
            this.trackIRObject = null;
            alert("Problem finding API");
        }
    }
    this.tirSetNote();

}